package com.booter.ws.controller;

import com.booter.ws.es.EsRestClient;
import com.booter.ws.es.model.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RequestMapping("es")
@RestController
public class EsController {

    @Autowired
    private EsRestClient restClient;

    @PostMapping("idx/rebuild")
    public Object rebuild(@RequestParam String idx, @RequestParam String properties) {
        return restClient.rebuild(idx, properties);
    }

    @PostMapping("idx/create")
    public Object createIdx(@RequestParam String idx, @RequestParam String properties) {
        return restClient.createIndex(idx, properties);
    }

    @PostMapping("idx/delete")
    public Object delIndex(@RequestParam String idx) {
        return restClient.deleteIndex(idx);
    }

    @PostMapping("idx/detail")
    public Object detail(@RequestParam String idx) {
        return restClient.detail(idx);
    }


    @RequestMapping("data/create")
    public Object create(@RequestBody Book book) {
        return restClient.create("idx_es_book", book);
    }


    @PostMapping("data/acct/search")
    public Object search(@RequestParam String idx, @RequestParam String name) throws IOException {
        return restClient.search(idx, name);
    }

    @PostMapping("data/acct/page/search")
    public Object page(@RequestParam String idx,
                       @RequestParam(defaultValue = "1", required = false) int page,
                       @RequestParam(defaultValue = "20", required = false) int size) throws IOException {
        return restClient.page(idx, page, size);
    }


    @PostMapping("data/acct/completion/suggest/search")
    public Object completionSuggesterSearch(@RequestParam String idx, @RequestParam String name) throws IOException {
        return restClient.completionSuggesterSearch(idx, "email", name);
    }

    @PostMapping("data/term/suggest/search")
    public Object termSuggesterSearch(@RequestParam String idx, @RequestParam String name) throws IOException {
        return restClient.termSuggesterSearch(idx, "body", name);
    }

    @PostMapping("data/phrase/suggest/search")
    public Object phraseSuggesterSearch(@RequestParam String idx, @RequestParam String name) throws IOException {
        return restClient.phraseSuggesterSearch(idx, "body", name);
    }

//    @RequestMapping("/data/batch/create")
//    public void batchCreate() throws Exception {
//        File file = ResourceUtils.getFile("/Users/chunwenli/Downloads/accounts.json");
//        List<String> strings = IOUtils.readLines(new FileInputStream(file), StandardCharsets.UTF_8);
//        int i = 0;
//        Long id = 0L;
//        LocalDateTime now = LocalDateTime.now();
//        Integer dataTime = Integer.valueOf(now.format(DateTimeFormatter.ofPattern("yyyyMMddHH")));
//        Long timestamp = now.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
//        List<Accounts> maps = new ArrayList<>();
//        for (String str : strings) {
//            JSONObject obj = JSONObject.parseObject(str);
//            if (i % 2 == 0) {
//                id = obj.getJSONObject("index").getLong("_id");
//            } else {
//                obj.put("id", id);
//                obj.put("dataTime", dataTime);
//                obj.put("createTime", timestamp);
//                obj.put("updateTime", timestamp);
//                Accounts a = new Accounts();
//                BeanUtils.populate(a, obj);
//                maps.add(a);
//            }
//            i++;
//        }
//        restClient.batchCreate("idx_accounts", maps);
//    }

}
